package com.coder4j.platform.service;

import com.coder4j.platform.config.GlobalConfig;
import com.coder4j.starter.tools.util.ListUtils;
import com.coder4j.starter.tools.vo.BaseException;
import com.coder4j.starter.web.BaseServiceImpl;
import com.coder4j.platform.entity.AuthOrganizationEntity;
import com.coder4j.platform.mapper.AuthOrganizationMapper;
import com.coder4j.platform.vo.AuthOrganizationQuery;
import com.coder4j.platform.vo.AuthOrganizationTreeNode;
import com.coder4j.platform.vo.AuthOrganizationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 组织
 * @author keller
 * @date 2024-05-09 11:08:09
 */
@Service
@Slf4j
public class AuthOrganizationService extends BaseServiceImpl<AuthOrganizationMapper, AuthOrganizationEntity, AuthOrganizationVO>  {

    @Resource
    private AuthOrganizationMapper mapper;

    @Override
    public AuthOrganizationVO entity2Vo(AuthOrganizationEntity entity) {
        AuthOrganizationVO vo = new AuthOrganizationVO();
        BeanUtils.copyProperties(entity,vo);
        vo.setTitle(entity.getName());
        vo.setKey(entity.getId());
        return vo;
    }

    @Override
    public AuthOrganizationEntity vo2Entity(AuthOrganizationVO vo) {
        AuthOrganizationEntity entity = new AuthOrganizationEntity();
        BeanUtils.copyProperties(vo, entity);
        return entity;
    }

    public List<AuthOrganizationVO> tree() {
        List<AuthOrganizationVO> voList = this.baseQueryVOList(new AuthOrganizationQuery());

        if(ListUtils.isEmpty(voList)){
            return voList;
        }
        for (AuthOrganizationVO vo : voList) {
            vo.setTitle(vo.getName());
            vo.setKey(vo.getId());
        }

        AuthOrganizationVO root = new AuthOrganizationVO();
        root.setId(GlobalConfig.RootId);

        this.baseInitChildren(root,voList);
        return root.getChildren();
    }

    public List<AuthOrganizationTreeNode> getTreeSelector(Long exclusionId) {
        List<AuthOrganizationEntity> list = this.baseQueryList(new AuthOrganizationQuery());
        List<AuthOrganizationTreeNode> voList = new ArrayList<>();

        if(ListUtils.isEmpty(list)){
            return voList;
        }

        //如果指定了要排除的组织ID,进行过滤
        if(exclusionId != null){
            list = list.stream().filter(item -> !item.getId().equals(exclusionId)).collect(Collectors.toList());
        }

        for (AuthOrganizationEntity item : list) {
            AuthOrganizationTreeNode vo = new AuthOrganizationTreeNode();
            vo.setId(item.getId());
            vo.setKey(item.getId() + "");
            vo.setParentId(item.getParentId());
            vo.setLabel(item.getName());
            vo.setValue(item.getId());
            voList.add(vo);
        }

        AuthOrganizationTreeNode root = new AuthOrganizationTreeNode();
        root.setId(GlobalConfig.RootId);
        root.setKey(root.getId() + "");

        this.baseInitChildren(root,voList);
        return root.getChildren();
    }

    public void save(AuthOrganizationVO vo) {
        AuthOrganizationEntity entity = this.vo2Entity(vo);
        String parentLevel = GlobalConfig.RootLevelPath;

        //默认为一级组织
        if (entity.getParentId() == null) {
            entity.setParentId(GlobalConfig.RootId);
        }

        Long parentId = entity.getParentId();

        //校验父节点
        if (!GlobalConfig.RootId.equals(parentId)) {
            AuthOrganizationEntity parent = this.baseGetById(parentId);
            if (parent == null) {
                throw BaseException.error("父组织不存在");
            }
            parentLevel = parent.getUniquePath();
        }
        //更新
        if (entity.getId() != null) {
            //ID和parentId一致，有问题
            if (entity.getId().equals(entity.getParentId())) {
                throw BaseException.errorData();
            }
            entity.setUniquePath(parentLevel + "_" + entity.getId());
            this.baseUpdateById(entity);
        } else {
            //添加，先保存数据，生成自增ID，然后将自增ID更新到层级路径上
            entity = this.baseSave(entity);
            entity.setUniquePath(parentLevel + "_" + entity.getId());
            this.baseUpdateById(entity);
        }

    }

}
